<?php

namespace app\admin\controller\dormitory;

use app\admin\model\DormitoryBedExchange;
use app\admin\model\DormitoryDorm;
use app\admin\model\DormitoryDormBed;
use app\common\controller\Backend;
use app\common\library\Auth;
use app\common\model\BehaviorRecord;
use think\Db;

class Album extends Backend
{
    /**
     * @var \app\admin\model\DormitoryDorm
     */
    protected $model = null;
    protected $searchFields = '';
    protected $noNeedRight = ['detail'];

    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('DormitoryDorm');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->alias('a')
                ->where('a.status', 1)
                ->where($where)
                ->count();

            $list = $this->model
                ->alias('a')
                ->join('__BEHAVIOR_RECORD__ b', 'b.garden_number = a.garden_number and b.building_number = a.building_number and b.dorm_number = a.number', 'left')
                ->join('__BEHAVIOR_RECORD_IMAGE__ c', 'c.record_id = b.id', 'left')
                ->join('__DORMITORY_GARDEN__ d', 'd.number = a.garden_number', 'left')
                ->join('__DORMITORY_BUILDING__ e', 'e.number = a.building_number and e.garden_number = a.garden_number', 'left')
                ->where('a.status', DormitoryDorm::NORMAL)
                ->where($where)
                ->field('a.*,count(c.id) image_count,d.name as garden_name,e.name as building_name')
                ->order($sort, $order)
                ->group('a.garden_number,a.building_number,a.number')
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

}
